package com.sun.reggie.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sun.reggie.entity.Dish;
import com.sun.reggie.entity.DishFlavor;
import com.sun.reggie.entity.dto.DishDto;
import com.sun.reggie.mapper.DishMapper;
import com.sun.reggie.service.DishFlavorService;
import com.sun.reggie.service.DishService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.stream.Collectors;

@Service
@Slf4j
public class DishServiceImpl extends ServiceImpl<DishMapper,Dish> implements DishService {
    @Autowired
    private DishFlavorService dishFlavorService;

    @Autowired
    private DishService dishService;
    /**
     * 新增菜品，同时保存对应的口味数据
     * @param dishDto
     */
    @Transactional
    public void saveWithFlavor(DishDto dishDto) {
        //保存菜品的基本信息到菜品表dish
        this.save(dishDto);

        Long dishId = dishDto.getId();//菜品id
        //菜品口味
        List<DishFlavor> flavors = dishDto.getFlavors();
        flavors = flavors.stream().map((item) -> {
            item.setDishId(dishId);
            return item;
        }).collect(Collectors.toList());

        //保存菜品口味数据到菜品口味表dish_flavor
        dishFlavorService.saveBatch(flavors);
    }


    /**
     * 分页查询增强性菜品对象DTO
     * @param dishDtoPage
     * @param name
     * @return
     */
    @Override
    public Page<DishDto> pageDishDto(Page<DishDto> dishDtoPage, String name) {
        return this.getBaseMapper().getDishDtoList(dishDtoPage,name);
    }

    /**
     * 根据id查询菜品信息和对应的口味信息
     * @param id
     * @return
     */
    @Override
    public DishDto getByIdWithFlavor(Long id) {

        Dish dish= this.getById(id);
        DishDto dishDto = new DishDto();

        BeanUtils.copyProperties(dish,dishDto);

        LambdaQueryWrapper<DishFlavor> dishFlavorLambdaQueryWrapper = new LambdaQueryWrapper<>();
        dishFlavorLambdaQueryWrapper.eq(DishFlavor::getDishId,id);

        List<DishFlavor> list = dishFlavorService.list(dishFlavorLambdaQueryWrapper);

//        LambdaQueryWrapper<Category> categoryLambdaQueryWrapper = new LambdaQueryWrapper<>();
//        categoryLambdaQueryWrapper.eq(Category::getId,dish.getCategoryId());
//
//        Category one = categoryService.getOne(categoryLambdaQueryWrapper);

        dishDto.setFlavors(list);
//        dishDto.setCategoryName(one.getName());

        return dishDto;
    }

    @Override
    @Transactional
    public void updateWithFlavor(DishDto dishDto) {

        this.updateById(dishDto);
        LambdaQueryWrapper<DishFlavor> dishFlavorLambdaQueryWrapper = new LambdaQueryWrapper<>();
        dishFlavorLambdaQueryWrapper.eq(DishFlavor::getDishId,dishDto.getId());
        dishFlavorService.remove(dishFlavorLambdaQueryWrapper);

        List<DishFlavor> flavors = dishDto.getFlavors();
        List<DishFlavor> collect = flavors.stream().map(s -> {
            s.setDishId(dishDto.getId());
            return s;
        }).collect(Collectors.toList());

        dishFlavorService.saveBatch(collect);

    }

    @Override
    @Transactional
    public boolean removeByIdsWithFlavor(List<Long> ids) {


        boolean b= dishService.removeByIds(ids);

        LambdaQueryWrapper<DishFlavor> dishFlavorLambdaQueryWrapper = new LambdaQueryWrapper<>();
        for (Long id : ids) {
            dishFlavorLambdaQueryWrapper.eq(DishFlavor::getDishId,id);
            boolean b1 = dishFlavorService.remove(dishFlavorLambdaQueryWrapper);
        }

        return true;
    }

    @Override
    public Page<DishDto> pageRecycleDishDto(Page<DishDto> dishDtoPage, String name) {
        return this.getBaseMapper().pageRecycleDishDtoList(dishDtoPage,name);
    }

    @Override
    @Transactional
    public boolean recycleByIdsWithFlavor(List<Long> ids) {

        for (Long id : ids) {
            this.getBaseMapper().updateDish(id);
            this.getBaseMapper().updateDishFlavor(id);
        }

        return true;
    }
}
//        List<Dish> dishes = dishService.listByIds(ids);
//        dishes.stream().map(s->{
//            s.setIsDeleted(0);
//            return s;
//        }).collect(Collectors.toList());
//
//        LambdaQueryWrapper<DishFlavor> dishFlavorLambdaQueryWrapper = new LambdaQueryWrapper<>();
//        for (Long id : ids) {
//            dishFlavorLambdaQueryWrapper.eq(DishFlavor::getDishId,id);
//            List<DishFlavor> list = dishFlavorService.list(dishFlavorLambdaQueryWrapper);
//            list.stream().map(s->{
//                s.setIsDeleted(0);
//                return s;
//            }).collect(Collectors.toList());
//            dishFlavorService.updateBatchById(list);
//        }
//
//        dishService.updateBatchById(dishes);
//
//        return true;
